from EscSentinel2Download import DownloadSentinel2
from EscSentinel2Download import ShapeFile, RasterFile
from EscSentinel2Download import ShapeFileGeographyTransformProject
from EscSentinel2Download import ShapeFileProjectTransformGeography
from EscSentinel2Download import ConfigJsonFile
import json
import os


def GetGeographySentinel2():
    chinaProvinceShapeFilePath = r"E:\localhosttestdata\中国省级行政区划_shp"
    worldSentinel2ShapeFilePath = r"E:\localhosttestdata\MGRS_100kmSQ_ID_shp"
    geographyWorldSentinel2ShapeFilePath = r"E:\localhosttestdata\geographyworldsentinel2_2"
    chinaProvinceLayerName = ShapeFile.LayerIndexToLayerName(chinaProvinceShapeFilePath)
    DownloadSentinel2.GetGeographyWorldSentinel2ShapeFile(
        chinaProvinceShapeFilePath,
        chinaProvinceLayerName,
        worldSentinel2ShapeFilePath,
        geographyWorldSentinel2ShapeFilePath)
    return


def GetEffectiveGeographySentinel2():
    geographyWorldSentinel2ShapeFilePath = r"E:\localhosttestdata\geographyworldsentinel2_2"
    singleLayerShapeFilePath = r"E:\localhosttestdata\geographysinglelayerworldsentinel2"
    singleLayerName = r"worldsentinel2"
    DownloadSentinel2.ComposeSingleLayerShapeFile(
        geographyWorldSentinel2ShapeFilePath, singleLayerShapeFilePath, singleLayerName)
    return


def CountySentinel2ZoneShapeFile():
    singleLayerShapeFilePath = r"E:\localhosttestdata\geographysinglelayerworldsentinel2"
    countyShapeFilePath = r"E:\localhosttestdata\hebeixianyu"
    DownloadSentinel2.CountySentinel2ZoneShapeFile(
        countyShapeFilePath, singleLayerShapeFilePath)
    return


def GetConfigFileParams():
    currentFolderPath = os.getcwd()
    # configJsonFilePath = currentFolderPath + r"s2zoneconfig.json"
    configJsonFilePath = os.path.join(currentFolderPath, r"s2zoneconfig.json")
    with open(configJsonFilePath, "r") as jsonfile:
        configdict = json.load(jsonfile)
    return configdict


def DelTempJpgFile(jpgFolderPath):
    """
    清空文件夹下的jpg文件
    :param jpgFolderPath:
    :return:
    """
    directoryNameList = os.listdir(jpgFolderPath)
    partten = re.compile(".*\.jpg")
    fileNameList = []
    for directoryName in directoryNameList:
        if partten.search(directoryName):
            jpgFilePath = os.path.join(jpgFolderPath, directoryName)
            os.remove(jpgFilePath)
    return


def DownLoad():
    # saveFolderPath = "E:\localhosttestdata"
    # shapeFilePath = r"E:\localhosttestdata\hebeixianyu"
    # countyCode = "130208"
    configdict = GetConfigFileParams()
    saveFolderPath = configdict["saveFolderPath"]
    shapeFilePath = configdict["shapeFilePath"]
    producttype = configdict["producttype"]
    username = configdict["username"]
    password = configdict["password"]
    url = configdict["url"]
    maxcloudcoverpercentage = configdict["maxcloudcoverpercentage"]
    # saveFolderPath = input("请输入快图保存文件夹路径:")
    countyCode = input("请输入县区划代码:  ")
    startTime = input("请输入影像开始时间:  ")
    endTime = input("请输入影像结束时间:  ")
    # maxcloudcoverpercentageStr = input("请输入最大含云量:默认(30)")
    # maxcloudcoverpercentage = int(maxcloudcoverpercentageStr)
    period =(startTime, endTime)
    s2ZoneList = DownloadSentinel2.CountyCodeToSentinel2Zone(countyCode, shapeFilePath)
    print("需要下载的哨兵图幅:", s2ZoneList)
    api = DownloadSentinel2.GetEsaWebApi(username, password, url)
    kwargs = {
        "date": period,
        "platformname": 'Sentinel-2',
        "producttype": producttype,  # S2MSI1C
        "cloudcoverpercentage": (0, maxcloudcoverpercentage)}
    productAllOrderdDict = DownloadSentinel2.CheckProductByMGRS(api, s2ZoneList, kwargs)
    productInfoDictList = DownloadSentinel2.GetProductInfo(api, productAllOrderdDict, saveFolderPath)
    DelTempJpgFile(saveFolderPath)
    DownloadSentinel2.DownloadProductInfoQuickImage(
        productInfoDictList, saveFolderPath, username, password)
    downloadFlag = input("请删除不需要的影像!\n删除完毕请按: 1\n退出下载请按其他键!\n")
    if downloadFlag == "1":
        fileNameList = DownloadSentinel2.FromFolderSelectS2ZoneNoneList(saveFolderPath)
        productAllOrderdDict = DownloadSentinel2.CheckProductByFileName(api, fileNameList)
        DownloadSentinel2.DownloadMultipleProduct(api, productAllOrderdDict)
    else:
        return
    return


def DownLoad_1():
    # saveFolderPath = "E:\localhosttestdata"
    # shapeFilePath = r"E:\localhosttestdata\hebeixianyu"
    # countyCode = "130208"
    configdict = GetConfigFileParams()
    saveFolderPath = configdict["saveFolderPath"]
    shapeFilePath = configdict["shapeFilePath"]
    producttype = configdict["producttype"]
    username = configdict["username"]
    password = configdict["password"]
    url = configdict["url"]
    maxcloudcoverpercentage = configdict["maxcloudcoverpercentage"]
    # saveFolderPath = input("请输入快图保存文件夹路径:")
    countyCode = input("请输入县区划代码:  ")
    startTime = input("请输入影像开始时间:  ")
    endTime = input("请输入影像结束时间:  ")
    # maxcloudcoverpercentageStr = input("请输入最大含云量:默认(30)")
    # maxcloudcoverpercentage = int(maxcloudcoverpercentageStr)
    period =(startTime, endTime)
    s2ZoneList = DownloadSentinel2.CountyCodeToSentinel2Zone(countyCode, shapeFilePath)
    print("需要下载的哨兵图幅:", s2ZoneList)
    api = DownloadSentinel2.GetEsaWebApi(username, password, url)
    kwargs = {
        "date": period,
        "platformname": 'Sentinel-2',
        "producttype": producttype,  # S2MSI1C
        "cloudcoverpercentage": (0, maxcloudcoverpercentage)}
    productAllOrderdDict = DownloadSentinel2.CheckProductByMGRS(api, s2ZoneList, kwargs)
    productInfoDictList = DownloadSentinel2.GetProductInfo(api, productAllOrderdDict, saveFolderPath)
    DelTempJpgFile(saveFolderPath)
    DownloadSentinel2.DownloadProductInfoQuickImage(
        productInfoDictList, saveFolderPath, username, password)
    downloadFlag = input("请删除不需要的影像!\n删除完毕请按: 1\n退出下载请按其他键!\n")
    if downloadFlag == "1":
        fileNameList = DownloadSentinel2.FromFolderSelectS2ZoneNoneList(saveFolderPath)
        productAllOrderdDict = DownloadSentinel2.CheckProductByFileName(api, fileNameList)
        DownloadSentinel2.DownloadMultipleProduct(api, productAllOrderdDict)
    else:
        return
    return


def main():
    DownLoad()
    return


if __name__ == "__main__":
    main()